#!/usr/bin/perl
# Sep 8, 2009 Filtering SNP

use strict;
use warnings;
use IO::File;
use Getopt::Std;
use Carp qw(croak);

if(@ARGV != 1 or $ARGV[0] eq '--help'){
	die "Usage: $0 [Maq snip file]\n";
}
my $FILE = shift;
my $fh = IO::File->new($FILE) or die "cannot open $FILE: $!";
my %data_best;
my %data_other;
while($_ = $fh->getline()){
	s/\r?\n//;
	my $line = $_;
	my ($ch, $posi, $ref, $consensus, $phred, $readDepth, $repeat, $quality, $minQuality, $second, $ratio, $thrid) = split /\t/;
	my $key = "$ch$posi";
	unless($consensus =~ m/[ATGC]/){
		next;
	}
	if($repeat != 1.00){
		next;
	}
	if($second eq 'N' and $thrid eq 'N'){
		$data_best{$key}{data} = $line;
		$data_best{$key}{depth} = $readDepth;
	}else{
		$data_other{$key}{data} = $line;
		$data_other{$key}{depth} = $readDepth;
	}
}
$fh->close();

foreach my $key (sort {$data_best{$b}{depth} <=> $data_best{$a}{depth}} keys %data_best){
	print "$data_best{$key}{data}\n";
}
foreach my $key (sort {$data_other{$b}{depth} <=> $data_other{$a}{depth}} keys %data_other){
	print "$data_other{$key}{data}\n";
}
		 